package com.guodi.common.exception;

import com.guodi.common.constant.StatusCode;
import com.guodi.core.tool.api.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Objects;

/**
 * 异常拦截
 */
@Slf4j
@RestControllerAdvice
public class OneMapRestExceptionTranslator {

    /**
     * 捕获BaseRuntimeException 异常，返回的http状态码是200的，只是ackCode是500
     * */
    @ExceptionHandler({BaseRuntimeException.class})
    public R handleError(BaseRuntimeException e) {
        log.error("业务异常：" + (Objects.nonNull(e.getLogMessage())? e.getLogMessage() : e.getMessage()) , e);
        return R.fail(StatusCode.ERROR_CODE_40005, e.getMessage());
    }
}
